package com.wbs.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.wbs.mapper.TbItemCatMapper;
import com.wbs.model.TbItem;
import com.wbs.model.TbItemCat;
import com.wbs.model.TbItemCatExample;
import com.wbs.service.ItemCatService;
import com.wbs.service.SpecificationOptionService;
import com.wbs.service.TypeTemplateService;
import com.wbs.vo.ItemCatVo;
import com.wbs.vo.SpecificationOptionVo;
import com.wbs.vo.SpecificationVO;
import com.wbs.vo.TypeTemplateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 芝梦
 * @Description:
 * @DateTime: 2024/1/29 20:06
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Autowired
    private TypeTemplateService typeTemplateService;

    @Autowired
    private SpecificationOptionService specificationOptionService;

    //通过ParentId查找
    @Override
    public List<ItemCatVo> findByParentId(Long parentId) {
        ItemCatVo itemCatVo = new ItemCatVo();
        itemCatVo.setParentId(parentId);
        //使用buildExample()方法进行模糊查询 然后 利用convertToVo()方法进行类型转换
        List<TbItemCat> tbItemCatList = tbItemCatMapper.selectByExample(this.buildExample(itemCatVo));
        return tbItemCatList.stream().map(this::convertToVo).collect(Collectors.toList());
//        return tbItemCatList.stream().map(tbItemCat -> {return this.convertToVo(tbItemCat);}).collect(Collectors.toList());

    }

    @Override
    public TypeTemplateVo getTypetemplate(Long id) {
        //根据id查询分类对象
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
        TypeTemplateVo typeTemplateVo = typeTemplateService.selectByKey(tbItemCat.getTypeId());

        //封装规格和规格组成数据
        String specIds = typeTemplateVo.getSpecIds();
        // 规格集合 列 : [{"id":27,"text":"网络"},{"id":32,"text":"机身内存"}]
        List<SpecificationVO> specificationVOS = JSONObject.parseArray(specIds, SpecificationVO.class);

        //查询所有的规格组成数据
        List<SpecificationOptionVo> optionVos = specificationOptionService.findAllOption();
        //将所有的规格组成数据按照specId进行分组 也就是按照规格的id进行分组
        Map<Long, List<SpecificationOptionVo>> map = optionVos.stream().collect(Collectors.groupingBy(x ->x.getSpecId()));

        //设置规格集合中的规格组成集合
        for (SpecificationVO specificationVO : specificationVOS) {
            //将分组好的规格组成集合按照id分配给对应规格集合
            Long key = specificationVO.getId();
            List<SpecificationOptionVo> specificationOptionVos = map.get(key);
            specificationVO.setList(specificationOptionVos);
        }

        //将规格集合设置进模板管理Vo中
        typeTemplateVo.setSpecificationVOList(specificationVOS);

        return typeTemplateVo;
    }

    /* 以下是自定义的辅助方法------------------------------------------------------------------------------------------------------------------------------*/

    //构建条件类
    private TbItemCatExample buildExample(ItemCatVo itemCatVo) {
        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = tbItemCatExample.createCriteria();
        if (itemCatVo != null) {
            criteria.andParentIdEqualTo(itemCatVo.getParentId());
        }
        return tbItemCatExample;
    }

    // TbItemCat 转换 ItemCatVo
    private ItemCatVo convertToVo(TbItemCat tbItemCat) {
        ItemCatVo itemCatVo = new ItemCatVo();
        BeanUtils.copyProperties(tbItemCat, itemCatVo);
        return itemCatVo;
    }

    // ItemCatVo 转换 TbItemCat
    private TbItemCat convertToTb(ItemCatVo itemCatVo) {
        TbItemCat tbItemCat = new TbItemCat();
        BeanUtils.copyProperties(itemCatVo, tbItemCat);
        return tbItemCat;
    }
}
